﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace editor.app
{
    /// <summary>
    /// RKFAnimView.xaml 的交互逻辑
    /// </summary>
    public partial class RKFAnimView : UserControl
    {
        public int MaxCentreX { get; private set; }
        public int MaxCentreY { get; private set; }

        public RKFAnimView()
        {
            InitializeComponent();
        }

        public void Load(comm.objst.RPack rpack, string kfanimResKey, string imageKey)
        {
            var kfanim = rpack.GetResource<comm.objst.RKeyFrameAnim>(kfanimResKey);
            if (kfanim != null && kfanim.ImageDic.ContainsKey(imageKey))
            {
                var image = kfanim.ImageDic[imageKey];
                var rimage = rpack.GetResource<comm.objst.RImage>(image.ResKey);
                if (rimage != null)
                {
                    var bitmap = new BitmapImage();
                    if (rimage.ImageBytes != null)
                    {
                        bitmap.FromBytes(rimage.ImageBytes);
                    }

                    MaxCentreX = 0;
                    MaxCentreY = 0;
                    var frameList = new List<comm.objst.RImage.Frame>();
                    foreach (var frameIndex in image.FrameList)
                    {
                        var frame = rimage.GetFrame(frameIndex);
                        if (frame != null)
                        {
                            MaxCentreX = Math.Max(frame.CentreX, MaxCentreX);
                            MaxCentreY = Math.Max(frame.CentreY, MaxCentreY);
                        }
                        frameList.Add(frame);
                    }
                    int mw = 0, mh = 0;
                    foreach (var frame in frameList)
                    {
                        mw = Math.Max((frame.Width - frame.CentreX), mw);
                        mh = Math.Max((frame.Height - frame.CentreY), mh);
                    }

                    cvRoot.Width = MaxCentreX + mw;
                    cvRoot.Height = MaxCentreY + mh;
                    cvFrames.Width = cvRoot.Width;
                    cvFrames.Height = cvRoot.Height;

                    var ix = 0;
                    foreach (var frame in frameList)
                    {
                        var cv = new Canvas();
                        if (frame != null)
                        {
                            cv.Width = frame.Width;
                            cv.Height = frame.Height;
                            var img = new Image { Source = bitmap, Width = bitmap.PixelWidth, Height = bitmap.PixelHeight };
                            img.SetLeftTop(-frame.X, -frame.Y);
                            cv.Children.Add(img);
                        }
                        else
                        {
                            cv.Width = frame.Width;
                            cv.Height = frame.Height;
                            cv.Children.Add(new Border { Child = new TextBlock { Text = "null" } });
                        }
                        cv.Children.Add(new Border
                        {
                            Child = new TextBlock { Text = ix.ToString() },
                            Background = new SolidColorBrush(Color2.White(50))
                        });
                        cv.Clip = new RectangleGeometry(new Rect(0, 0, cv.Width, cv.Height));
                        cv.SetLeftTop(MaxCentreX - frame.CentreX, MaxCentreY - frame.CentreY);
                        if (ix != 0)
                        {
                            cv.Visibility = Visibility.Collapsed;
                        }
                        cvFrames.Children.Add(cv);
                        ix++;
                    }

                    var CENTRE_DOT_SIZE = 9;
                    var dot = new Ellipse
                    {
                        Opacity = .6,
                        Width = CENTRE_DOT_SIZE,
                        Height = CENTRE_DOT_SIZE,
                        Fill = new SolidColorBrush(Color2.Red()),
                        Stroke = new SolidColorBrush(Color2.White()),
                        StrokeThickness = 1
                    };
                    dot.SetLeftTop(MaxCentreX - CENTRE_DOT_SIZE / 2, MaxCentreY - CENTRE_DOT_SIZE / 2);
                    cvRoot.Children.Add(dot);
                }
            }
        }

        public void EnterFrame(uint index)
        {
            var ix = 0;
            foreach (var item in cvFrames.Children)
            {
                var cv = (Canvas)item;
                cv.Visibility = ix == index ? Visibility.Visible : Visibility.Collapsed;
                ix++;
            }
        }
    }
}
